﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Applications.City.Shop;

namespace XingFuStudio.Pioneer.Applications.City.Element {
    /// <summary>
    /// 轮船信息实体
    /// </summary>
    public class ShipInfo : CityElement{
        /// <summary>
        /// 货物ID
        /// </summary>
        public int CargoId {
            get;
            private set;
        }

        /// <summary>
        /// 出航时间
        /// </summary>
        public DateTime StartTime {
            get;
            private set;
        }

        /// <summary>
        /// 是否可以收获
        /// </summary>
        public bool GetCanHarvest(List<ShipShopInfo> cargoes) {
            var res = cargoes.Where(s => s.ID == this.CargoId);

            if (res.Count() > 0) {
                ShipShopInfo ship = res.First() as ShipShopInfo;
                if (ship != null && this.StartTime != DateTime.MinValue) {
                    if (this.StartTime.AddMinutes(ship.TransTimes) < DateTime.Now) {
                        return true;
                    }
                }
            } else {
                if (this.StartTime != DateTime.MinValue && this.StartTime.AddDays(1) < DateTime.Now) {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 是否可以开船
        /// </summary>
        public bool CanStart {
            get;
            set;
        }


        /// <summary>
        /// 初始化元素自身数据
        /// </summary>
        /// <param name="elementObject"></param>
        public override void InitElement(Dictionary<string, object> elementObject) {
            //调用基类方法
            base.InitElement(elementObject);

            //赋值自身属性
            if ((string)elementObject["starttime"] == "0000-00-00 00:00:00") {
                this.StartTime = DateTime.MinValue;
            } else {
                this.StartTime = Convert.ToDateTime((string)elementObject["starttime"]);
            }
            this.CargoId = Convert.ToInt32((string)elementObject["cargoid"]);
            //this.CanHarvest = (string)elementObject["ustatus"] == "1";
            this.CanStart = (string)elementObject["ustatus"] == "0" && this.StartTime == DateTime.MinValue;
        }
    }
}
